package com.yhm.barcode;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public class GenerateCode128Barcode {
	public static void generateCode128(String code, String path)
			throws IOException, FileNotFoundException {
		File file = new File(path);
		Code128Bean bean = new Code128Bean();
		// int dpi = 150;

		bean.setModuleWidth(UnitConv.in2mm(0.006666666828095913D));
		bean.setHeight(10.0D);
		bean.doQuietZone(true);
		bean.setQuietZone(3.0D);

		bean.setFontName("Helvetica");
		bean.setFontSize(2.0D);

		bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);
		OutputStream out = new FileOutputStream(file);

		BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
				"image/jpeg", 150, 12, false, 0);
		bean.generateBarcode(canvas, code);

		canvas.finish();

		out.close();
	}
}
